
import { _decorator, Component, director, UIOpacity, Tween, Label, tween } from 'cc';
import { aaEvent } from '../frame/common/Enums';
const { ccclass, property } = _decorator;

@ccclass('ToastView')
export class ToastView extends Component {

    @property(Label) text: Label;
    @property(UIOpacity) toast: UIOpacity;

    private _tw = new Tween();
    private _time = 1;


    onEnable() {
        this.toast.opacity = 0;
        // this._tw.target(this.toast);
        // this._tw.set({ opacity: 0 }).to(0.6, { opacity: 255 }).delay(this._time).to(0.25, { opacity: 0 }).call(()=>{
        //     this.text.string = null;
        // });
        director.on(aaEvent.toast, this.showToast, this);
      
    }

    onDisable() {
        director.off(aaEvent.toast, this.showToast, this);
    }

    /* show toast */
    showToast(text, time = 2) {
        this._time = time;
        this.text.string = text;
        tween(this.toast).to(0.6, { opacity: 255 }).delay(this._time).to(0.4, { opacity: 0 }).call(()=>{
            this.text.string = "";
        }).start();
    }

}


